<?php
namespace App\Http\Controllers\Admin;

use App\Service\Admin\CxOrderService;
use App\Utilitys\Upload;
use Illuminate\Http\Request;

class CxOrderController extends BaseController
{
    //原创案例-导览

    public function __construct(CxOrderService $service)
    {
        $this->db = $service;
    }

    //列表
    public function getIndex(Request $request)
    {
        //header("Access-Control-Allow-Origin: *");
        $pages['start'] = $request->post('start', 1);
        $pages['limit'] = $request->post('limit', 10);
        $pas['orderno'] = $request->post('orderno', '');
        $pas['xqtitle'] = $request->post('xqtitle', '');
        $pas['seller'] = $request->post('seller', '');
        $res = $this->db->getIndex($pages, $pas);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data'], $res['total']);
    }

    //确认
    public function setSure(Request $request)
    {
        //header("Access-Control-Allow-Origin: *");
        $id = $request->post('id', 0);
        $res = $this->db->setSure($id);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }

    //完成
    public function setFinish(Request $request)
    {
        //header("Access-Control-Allow-Origin: *");
        $id = $request->post('id', 0);
        $res = $this->db->setFinish($id);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }

    //详情
    public function getOne(Request $request)
    {
        //header("Access-Control-Allow-Origin: *");
        $id = $request->post('id', 0);
        $res = $this->db->getOne($id);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data']);
    }

    //上传图片
    public function uploadImg(Request $request)
    {
        $id = $request->post('id', 0);
        $img = $request->post('img', '');
        if (!$img) {
            return $this->toJson(-2, '图片必须的');
        }
        $fpath = Upload::getPathByBase64($img);
        $res = $this->db->addFile($id, 1, $fpath);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }

    //上传视频
    public function uploadVideo(Request $request)
    {
        ini_set('upload_max_filesize', '100M');
        ini_set('post_max_size', '100M');
        $id = $request->post('id', 0);
        $upload = Upload::uploadVideo($request, 1, 1, time());
        if ($upload['code'] < 0) {
            return $this->toJson(-2, $upload['msg']);
        }
        $fpath = $upload['data'];
        $res = $this->db->addFile($id, 2, $fpath);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }

    //上传压缩包
    public function uploadZip(Request $request)
    {
        ini_set('upload_max_filesize', '100M');
        ini_set('post_max_size', '100M');
        $id = $request->post('id', 0);
        $upload = Upload::uploadZip($request);
        if ($upload['code'] < 0) {
            return $this->toJson(-2, $upload['msg']);
        }
        $fpath = $upload['data'];
        $res = $this->db->addFile($id, 3, $fpath);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }

//    //删除
//    public function del(Request $request)
//    {
//        //header("Access-Control-Allow-Origin: *");
//        $id = $request->post('id', 0);
//        $res = $this->db->del($id);
//        if ($res['code'] < 0) {
//            return $this->toJson(-2, $res['msg']);
//        }
//        return $this->toJson();
//    }
}